#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Written by Mr. HuChang

import requests
from lxml import etree
from urllib.parse import quote
import json


if __name__ == '__main__':
    url = "https://gkcx.eol.cn/school/31/professional"

    lua = '''function main(splash, args)
      splash.private_mode_enabled = false
      assert(splash:go("''' + url + '''"))
      assert(splash:wait(0.5))
      splash:set_user_agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36")
      return {
        html = splash:html(),
      }
    end'''

    response = requests.get("http://localhost:8888/execute?lua_source=" + quote(lua))
    html_text = json.loads(response.text)["html"]
    # print(len(html_text))

    html = etree.HTML(html_text)
    Map = {}

    infos = html.xpath('//*[@id="xueke_rank"]/div')[1:]
    for info in infos[:-1]:
        name = info.xpath('./table/tbody/tr/td[1]')[0].text
        subs = info.xpath('./table/tbody/tr/td[2]//text()')
        if name == '学科评估':
            dic = {}
            for sub in subs:
                data = sub.split('学')
                grade = data[0]
                num = data[1].strip('）').split('（')[-1]
                dic[grade] = int(num)
            Map[name] = dic
        else:
            Map[name] = subs
    dic = {}
    for sub in infos[-1].xpath('./table/tbody//tr')[1:]:
        name = sub.xpath('./td[1]')[0].text
        sub_name = sub.xpath('./td[2]//text()')
        dic[name] = sub_name
    Map['所有专业'] = dic
    print(Map)


